package com.zxl.yjb.activity.im;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zxl.yjb.GlideApp;
import com.zxl.yjb.R;
import com.zxl.yjb.adapter.MyFragmentCommentAdapter;
import com.zxl.yjb.app.Const;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.CommenBean;
import com.zxl.yjb.bean.CommentBean;
import com.zxl.yjb.bean.LoginSucessBean;
import com.zxl.yjb.http.HostUrl;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;
import com.zxl.yjb.utils.SaveBeanUtil;
import com.zxl.yjb.utils.ToastUtils;
import com.zxl.yjb.widget.CircleImageView;
import com.zxl.yjb.widget.flexbox.FlexboxLayout;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDetailActivity extends BaseActivity implements View.OnClickListener, OnRefreshLoadMoreListener {
    private RecyclerView rvCommentList;//评论列表
    private TextView tvPageTitle;
    private CircleImageView ivMyHeader;
    private TextView tvMyName;
    private TextView tvComment;
    private TextView tvScore;
    private FlexboxLayout flexboxEnsure;
    private View headerVeiw;
    private TextView tvMySignature;
    private MyFragmentCommentAdapter adapter;
    private int currentPage = 1;
    private int pageSize = 20;
    private SmartRefreshLayout refreshLayout;
    private ImageView ivBack;
    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_detail2);
        userId = getIntent().getStringExtra("userId");
        initView();
        initEvent();
        getUserInfo();
        initAdapter();
        getCommenList(false);
    }

    @Override
    protected void initView() {
        //title部分
        rvCommentList = findViewById(R.id.rv_comment_list);
        refreshLayout = findViewById(R.id.refresh_layout);
        tvPageTitle = findViewById(R.id.tv_page_title);
        ivBack = findViewById(R.id.iv_back);

        //评论列表以上部分
        headerVeiw = headerVeiw.inflate(this, R.layout.user_info_header, null);
        ivMyHeader = headerVeiw.findViewById(R.id.iv_my_header);
        tvMyName = headerVeiw.findViewById(R.id.tv_my_name);
        tvMySignature = headerVeiw.findViewById(R.id.tv_my_signature);
        tvComment = headerVeiw.findViewById(R.id.tv_comment);
        tvScore = headerVeiw.findViewById(R.id.tv_score);
        flexboxEnsure = headerVeiw.findViewById(R.id.flexbox_ensure);
        //初始化title部分的数据；
        tvPageTitle.setText(R.string.user_info);
    }
    private void getUserInfo() {
        DataManager dataManager = new DataManager();
        dataManager.getUserInfo(new ProgressObserver<LoginSucessBean>(new ObserverOnNextListener<LoginSucessBean>() {
            @Override
            public void onNext(LoginSucessBean loginSucessBean) {
                setUserInfo(loginSucessBean);

            }
        }, this),userId);
    }

    public void setUserInfo(LoginSucessBean loginSucessBean) {
        Object bean = SaveBeanUtil.getBean(this, Const.VIPINFO);
        if (bean == null) {
            loginSucessBean = (LoginSucessBean) bean;
        }
        tvMyName.setText(loginSucessBean.getNickname());
        tvMySignature.setText("uid:"+loginSucessBean.getUid());
        GlideApp.with(getApplication()).load(HostUrl.HOSTPIC + loginSucessBean.getFace_img()).into(ivMyHeader);
    }

    private void initAdapter() {
        //创建布局管理
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvCommentList.setLayoutManager(layoutManager);
        //创建适配器
        adapter = new MyFragmentCommentAdapter(R.layout.item_comment);
        adapter.addHeaderView(headerVeiw);
        //给RecyclerView设置适配器
        rvCommentList.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ToastUtils.show(getApplication(),position+"");
            }
        });
        refreshLayout.setOnRefreshLoadMoreListener(this);

    }


    private void getCommenList(final boolean isRefresh) {
        DataManager dataManager = new DataManager();
        Map<String,String> map = new HashMap<>();
        map.put("page",currentPage+"");
        map.put("count",pageSize+"");
        map.put("uid",userId+"");
        ObserverOnNextListener<CommentBean> listener = new ObserverOnNextListener<CommentBean>() {
            @Override
            public void onNext(CommentBean commenBean) {
                currentPage++;
                if (isRefresh) {
                    adapter.setNewData(commenBean.getEval_arr());
                    refreshLayout.finishRefresh();
                }else {
                    adapter.addData(commenBean.getEval_arr());
                    refreshLayout.finishLoadMore();
                    if (commenBean.getEval_arr().size()<pageSize){
                        refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                    }
                }
            }
        };
        dataManager.evaluate_other2uid_list(new ProgressObserver<CommentBean>(listener,UserDetailActivity.this){
            @Override
            public void showDialog() {
            }
        },map);

    }
    
    protected void initEvent() {
        ivBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }



    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore(5000);
        getCommenList(false);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh(5000);
        currentPage=1;
        getCommenList(true);
        refreshLayout.setNoMoreData(false);
    }


}
